package com.pig4cloud.pig.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.entity.MetaEntityEntity;
import com.pig4cloud.pig.admin.api.entity.MetaFieldEntity;
import com.pig4cloud.pig.admin.entity.Field;
import com.pig4cloud.pig.admin.mapper.MetaFieldMapper;
import com.pig4cloud.pig.admin.service.CommonService;
import com.pig4cloud.pig.admin.service.MetaEntityService;
import com.pig4cloud.pig.admin.service.MetaFieldService;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.constant.enums.FieldType;
import com.pig4cloud.pig.common.core.util.R;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 字段表
 *
 * @author pig
 * @date 2024-09-23 17:31:09
 */
@Service
public class MetaFieldServiceImpl extends ServiceImpl<MetaFieldMapper, MetaFieldEntity> implements MetaFieldService {
	private final CommonService commonService;

	private final MetaEntityService metaEntityService;

	public MetaFieldServiceImpl(CommonService commonService, @Lazy MetaEntityService metaEntityService) {
		this.commonService = commonService;
		this.metaEntityService = metaEntityService;
	}

	@Override
	public Page<MetaFieldEntity> queryMetaFieldEntityPage(Page page, MetaFieldEntity metaField) {
		return baseMapper.queryMetaFieldEntityPage(page, metaField);
	}

	@Override
	public Boolean removeMetaField(Long[] ids) {
		List<MetaFieldEntity> metaFieldEntityList = baseMapper.queryDelDataMetaField(ids);
		metaFieldEntityList.stream().findFirst().ifPresent(metaFieldEntity -> {
			MetaEntityEntity entity = metaEntityService.getById(metaFieldEntity.getEntityId());
			metaFieldEntityList.forEach(field -> commonService.removeTableField(entity.getPhysicalName(), field.getPhysicalName()));
		});
		return baseMapper.removeMetaField(ids);
	}

	@Override
	public Boolean reduction(Long id) {
		return baseMapper.reduction(id);
	}

	@Override
	public MetaFieldEntity getMetaFieldEntityByLabel(MetaFieldEntity metaField) {
		return baseMapper.getMetaFieldEntityByLabel(metaField);
	}

	@Override
	public R<?> saveMetaFieldEntity(MetaFieldEntity metaField) {
		if (ObjectUtil.isNull(metaField.getLabel())) {
			return R.failed("字段名称不能为空");
		}
		MetaEntityEntity entity = metaEntityService.getById(metaField.getEntityId());
		if (ObjectUtil.isNull(entity)) {
			return R.failed("实体不存在");
		}

		MetaFieldEntity field = getMetaFieldEntityByLabel(metaField);
		if (Objects.nonNull(field)) {
			return ObjectUtil.equal(field.getDelFlag(), CommonConstants.NO.toString()) ? R.failed("字段名称:" + field.getLabel() + "已存在") : R.failed("字段名称:" + field.getLabel() + "在回收站里，未删除");
		}
		metaField.setChildEntityFieldFlag(null);
		metaField.setParentIdFieldFlag(null);
		metaField.setPhysicalName(metaField.getLabel());
		metaField.setSystemFieldFlag(null);
		commonService.addTableField(
				entity.getPhysicalName(),
				new Field(metaField.getPhysicalName(),
						FieldType
								.getByValue(metaField.getType())
								.getType(),
						metaField.getName()));
		return R.ok(save(metaField));
	}
}
